package cn.mymall.entity.mall;

import cn.mymall.constant.MallConstant;
import cn.mymall.util.DateUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Data
@EqualsAndHashCode(callSuper = true)
@TableName("mall_sub_category")
public class SubCategory extends Model {
	private static final long serialVersionUID = 1107079891614674447L;
	@TableId(value = "id",type = IdType.AUTO)
	private Integer id;
	private Integer categoryId;
	private String name;
	private Integer sortNum;
	//可次日收货
	private String lastHour;
	//最早送到时间
	private String deliveryStartHour;
	//最晚送达时间
	private String deliveryEndHour;
	//送达间隔多少分钟
	private Integer intervalMinute;
	//需要生日信息
	private String needBirthday;

	private Timestamp addTime;
	public SubCategory(){

	}
	public List<String> gainDeliveryHourList(){
		if(intervalMinute==null){
			intervalMinute = 60;
		}
		String sdate = DateUtil.formatDate(DateUtil.getCurDate());
		List<String> hourList = null;
		if(StringUtils.isNotBlank(deliveryStartHour) && StringUtils.isNotBlank(deliveryEndHour)){
			hourList = new ArrayList<>();
			Timestamp start = DateUtil.parseTimestamp(sdate + " " + deliveryStartHour+":00");
			Timestamp end = DateUtil.parseTimestamp(sdate + " " + deliveryEndHour+":00");
			while(start.compareTo(end)<0){
				String s1 = DateUtil.format(start, "HH:mm");
				start = DateUtil.addMinute(start, intervalMinute);
				String s2 = DateUtil.format(start, "HH:mm");
				hourList.add(s1+"-"+s2);
			}
		}
		if(CollectionUtils.isEmpty(hourList) && categoryId.equals(MallConstant.CATEGORY_CAKE_ID)){
			hourList = new ArrayList<>();
			hourList.add("11:00-12:00");
			hourList.add("12:00-13:00");
			hourList.add("13:00-14:00");
			hourList.add("14:00-15:00");
			hourList.add("15:00-16:00");
			hourList.add("16:00-17:00");
			hourList.add("17:00-18:00");
			hourList.add("18:00-19:00");
		}
		return hourList;
	}
	public List<String> gainDeliveryDateList(){
		List<String> dateList = new ArrayList<>();
		Date curdate = new Date();
		String HHmm = DateUtil.format(curdate, "HH:mm");
		int x = 1;
		if(StringUtils.isNotBlank(lastHour) && lastHour.compareTo(HHmm)<=0){
			x = 2;
		}
		for(int i=x;i<=20;i++){
			dateList.add(DateUtil.formatDate(DateUtil.addDay(curdate, i)));
		}
		return dateList;
	}
}
